<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of taskObject
 *
 * @author nathaniel.stevens.ad
 */
require_once 'model/taskModelClass.php';

class taskControllerClass extends commonController {

    public function __construct($model) {
        $this->model = $model;
    }

    //ACTIONS*******************************************************************
    /*
     * Returns the task details page
     */
    public function viewTaskDetailsRequest() {
        $this->model->string = $this->model->getTaskDetailsPage();
    }

    /*
     * Returns the task form with empty fields for a new task
     */
    function proposeNewSubtaskRequest() {
        $this->model->string = $this->model->getNewSubtaskPage();
    }

    //****PROCESSING************************************************************

    /*
     * Main processing function
     */
    function processExistingTaskRequest() {
        if (isset($_POST['submit'])) {
            if (strcmp("Edit Task", $_POST['submit']) === 0) {
                $this->editExistingTaskRequest();
            } 
            else if (strcmp("Delete Task", $_POST['submit']) === 0) {
                $this->processDeleteTaskRequest();
            }             
            else if (strcmp("Deactivate", $_POST['submit']) === 0) {
                $this->processDeactivateTask();
            } 
            else if (strcmp("Activate", $_POST['submit']) === 0) {
                $this->processActivateTask();
            } 
            else if (strcmp("Add Notes", $_POST['submit']) === 0) {
                $this->processAddNotesRequest();
            }
            else {
                $this->processChangeTaskStatusRequest();
            }
        }
    }

    /*
     * Returns the task form with populated fields from an existing task
     */
    function editExistingTaskRequest() {
        $this->model->string = $this->model->getEditTaskPage();
    }
    
    /*
     *  Delete a task
     */
    private function processDeleteTaskRequest() {
        //
        //echo "Need to add deletion of req and param map to be able to delete task";
        //return;
        if (sizeof($this->model->subtasks) > 0) {
            $url = $_SERVER['HTTP_REFERER'];
            $url .= "&update=Cannot delete task delete subtask(s) first";
            echo header('location:'.$url);
        } else {
            $url = $this->model->processDeleteTask();
            if($url){
                echo header($url);
            }
            else{
                echo "Error deleting task";
            }
        }
    }

    private function processDeactivateTask() {
        echo 'deactivate task ';
        echo $this->model->ID;
    }

    private function processActivateTask() {
        echo 'activate task ';
        echo $this->model->ID;
    }

    /*
     * Change the status of a task
     */
    private function processChangeTaskStatusRequest() {
        $url = $this->model->processChangeTaskStatus();
        if ($url) {
            echo header($url);
        } else {
            echo "Error changing task status";
        }
    }

    public function toString() {
        $output = "";
        $output .= "WBS: $this->wbsString Title: $this->title Start: $this->startDate End: $this->endDate Status: $this->status ";
        $output .= "Duration: $this->duration Desc: $this->description PCost: $this->proposedCost <br>";
        if ($this->subtasks) {
            for ($i = 0; $i < sizeof($this->subtasks); $i++) {
                $curSubtask = $this->subtasks[$i];
                $output .= "$curSubtask->title <br>";
                $output .= $curSubtask->financialObject->toString();
            }
        }
        return $output;
    }

}

?>
